﻿Imports System.Math



Public Class Scheduler

    Public oDateToCheck As Date

    Dim oBindingSource As New BindingSource
    Dim iNbrCol As Integer
    Dim iIncrement As Integer
    Dim NeedValidate As Boolean
    Dim LockValidating As Boolean
    Dim oLocalContext As DBMainContext
    Dim oLocalListHoraire As New List(Of Horaire)
    Dim oComboColumNames As New DataGridViewComboBoxColumn




    Sub New()

        ' Cet appel est requis par le Concepteur Windows Form.
        InitializeComponent()
        'Ajoutez une initialisation quelconque après l'appel InitializeComponent().



    End Sub

    Sub Initialize(ByVal nbrCol As Integer, ByVal increment As Integer, ByVal oContext As DBMainContext, ByVal odate As Date)
        Dim header As String
        Dim iHeure As Integer = 0
        Dim iMinute As Integer = 0
        Dim oListEmploye As New List(Of Employe)
        oDateToCheck = odate
        Dim oTextBoxColumn As New DataGridViewTextBoxColumn
        oLocalContext = oContext
        iNbrCol = nbrCol
        iIncrement = increment
        LockValidating = True


        dgvSchedule.Columns.Add("ID", "ID")
        dgvSchedule.Columns("ID").Visible = False
        dgvSchedule.Columns("ID").Frozen = True

        oComboColumNames.Name = "Nom"
        oComboColumNames.HeaderText = "Nom"
        oComboColumNames.Frozen = True
        oListEmploye = (From u In oLocalContext.Employe Where u.Actif = True Order By u.Nom Select u).ToList


        oComboColumNames.DataSource = oListEmploye
        oComboColumNames.DisplayMember = "NomComplet"
        oComboColumNames.ValueMember = "ID"
        oComboColumNames.ValueType = GetType(Integer)
        oComboColumNames.Width = 120
        dgvSchedule.Columns.Add(oComboColumNames)

        oTextBoxColumn.Name = "strHoraire"
        oTextBoxColumn.Visible = False
        dgvSchedule.Columns.Add(oTextBoxColumn)


        'Initialise la grille
        For i = Outils.ParametreApplication.DebutDeLaJournee To nbrCol - 1
            header = GetHeader(i)
            dgvSchedule.Columns.Add(header, header)
            dgvSchedule.Columns(header).Width = 38
            'MsgBox(header)
        Next
        For i = 0 To Outils.ParametreApplication.DebutDeLaJournee - 1
            header = GetHeader(i)
            dgvSchedule.Columns.Add(header, header)
            dgvSchedule.Columns(header).Width = 38
            'MsgBox(header)
        Next
        cbQuart.SelectedIndex = 0

        Dim oParam As New Param

        pbDisponible.BackColor = Outils.ParametreApplication.CouleurDispo
        pbNdisponible.BackColor = Outils.ParametreApplication.CouleurNonDispo

        'dgvSchedule.DataSource = oLocalListHoraire

        'For Each column In (From u As DataGridViewColumn In dgvSchedule.Columns Where u.Index > iNbrCol + 1 Select u)
        '    column.Visible = False
        'Next
      
        LockValidating = False
    End Sub
    Sub setHoraire(ByVal oStrHoraire As String, ByVal RowID As Integer)

        If oStrHoraire Is Nothing Then
            oStrHoraire = "".PadLeft(96, "0")
        End If

        For j = Outils.ParametreApplication.DebutDeLaJournee To iNbrCol - 1
            CType(dgvSchedule.Rows(RowID).Cells(GetHeader(j)), DataGridViewTextBoxCell).Style.BackColor = GetColor(oStrHoraire(j))
        Next

        For j = 0 To Outils.ParametreApplication.DebutDeLaJournee - 1
            CType(dgvSchedule.Rows(RowID).Cells(GetHeader(j)), DataGridViewTextBoxCell).Style.BackColor = GetColor(oStrHoraire(j))
        Next
    End Sub

    Private Function GetColor(ByVal oChar As Char)
        Select Case oChar
            Case "0"
                Return pbNdisponible.BackColor
            Case "1"
                Return pbDisponible.BackColor
            Case "2"
                Return Outils.ParametreApplication.CouleurActivite
            Case "3"
                Return Outils.ParametreApplication.CouleurAccompagnement
            Case "4"
                Return Outils.ParametreApplication.CouleurTache
            Case Else
                Return Color.White
        End Select
    End Function

    Sub setDispo()
        If dgvSchedule.Columns.Count > 1 Then
            Dim oStrHoraire As String = Nothing
            Dim oDispoEmp As Disponibilite_Employe
            Dim iSelectedID As Integer = dgvSchedule.Rows(dgvSchedule.SelectedCells(0).RowIndex).Cells("Nom").Value
            oDispoEmp = (From u In oLocalContext.Disponibilite_Employe Where u.ID_Employe = iSelectedID Select u).FirstOrDefault
            If Not IsNothing(oDispoEmp) Then
                Select Case oDateToCheck.DayOfWeek
                    Case DayOfWeek.Monday
                        oStrHoraire = oDispoEmp.Lundi
                    Case DayOfWeek.Tuesday
                        oStrHoraire = oDispoEmp.Mardi
                    Case DayOfWeek.Wednesday
                        oStrHoraire = oDispoEmp.Mercredi
                    Case DayOfWeek.Thursday
                        oStrHoraire = oDispoEmp.Jeudi
                    Case DayOfWeek.Friday
                        oStrHoraire = oDispoEmp.Vendredi
                    Case DayOfWeek.Saturday
                        oStrHoraire = oDispoEmp.Samedi
                    Case DayOfWeek.Sunday
                        oStrHoraire = oDispoEmp.Dimanche
                    Case Else
                        oStrHoraire = "".PadLeft(96, "0")
                End Select
            End If

            If oStrHoraire Is Nothing Then
                oStrHoraire = "".PadLeft(96, "0")
            End If

            For j = Outils.ParametreApplication.DebutDeLaJournee To iNbrCol - 1
                If oStrHoraire(j) = "1" Then 'Disponible
                    CType(dgvSchedule.Rows(dgvSchedule.SelectedCells(0).RowIndex).Cells(GetHeader(j)), DataGridViewTextBoxCell).Style.BackColor = pbDisponible.BackColor
                End If
                If oStrHoraire(j) = "0" Then
                    CType(dgvSchedule.Rows(dgvSchedule.SelectedCells(0).RowIndex).Cells(GetHeader(j)), DataGridViewTextBoxCell).Style.BackColor = pbNdisponible.BackColor
                End If
            Next
            For j = 0 To Outils.ParametreApplication.DebutDeLaJournee - 1
                If oStrHoraire(j) = "1" Then 'Disponible
                    CType(dgvSchedule.Rows(dgvSchedule.SelectedCells(0).RowIndex).Cells(GetHeader(j)), DataGridViewTextBoxCell).Style.BackColor = pbDisponible.BackColor
                End If
                If oStrHoraire(j) = "0" Then
                    CType(dgvSchedule.Rows(dgvSchedule.SelectedCells(0).RowIndex).Cells(GetHeader(j)), DataGridViewTextBoxCell).Style.BackColor = pbNdisponible.BackColor
                End If
            Next
            dgvSchedule.Rows(dgvSchedule.SelectedCells(0).RowIndex).Cells(0).Value = New Horaire With {.strHoraire = oStrHoraire}

        End If



    End Sub

    Public Sub SetData(ByVal odate As Date)
        oLocalListHoraire = (From u In oLocalContext.Horaire Where CDate(u.Date).Date = odate.Date Select u).ToList

        For Each Hor In oLocalListHoraire
            dgvSchedule.Rows.Add(Hor)
            Dim oHor As Horaire = Hor

            With (From u As DataGridViewRow In dgvSchedule.Rows Where CType(u.Cells(0).Value, Horaire) Is oHor Select u).FirstOrDefault
                Dim oList As List(Of Employe) = CType(.Cells(1), DataGridViewComboBoxCell).DataSource
                If Not (From u In oList Select u.ID).Contains(CType(.Cells(0).Value, Horaire).Employe.ID) Then
                    oList.Add((From u In oLocalContext.Employe Where u.ID = CType(.Cells(0).Value, Horaire).Employe.ID Select u).FirstOrDefault)
                    CType(.Cells(1), DataGridViewComboBoxCell).DataSource = oList
                    CType(.Cells(1), DataGridViewComboBoxCell).DisplayMember = "NomComplet"
                    CType(.Cells(1), DataGridViewComboBoxCell).ValueMember = "ID"
                    .Cells(1).Value = CType(.Cells(0).Value, Horaire).Employe.ID
                    .Cells(1).ReadOnly = False
                Else
                    .Cells(1).Value = CType(.Cells(0).Value, Horaire).Employe.ID
                    .Cells(1).ReadOnly = True
                    setHoraire(CType(.Cells(0).Value, Horaire).strHoraire, .Index)
                End If



            End With
        Next

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim oAjoutEmploye As New AjoutEmployeHoraire
        oAjoutEmploye.ShowDialog()

    End Sub


    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbQuart.SelectedIndexChanged
        Select Case cbQuart.SelectedIndex
            Case 0
                dgvSchedule.FirstDisplayedCell = dgvSchedule.Item("6:00", 0)
            Case 1
                dgvSchedule.FirstDisplayedCell = dgvSchedule.Item("16:00", 0)
            Case 2
                dgvSchedule.FirstDisplayedCell = dgvSchedule.Item("22:00", 0)

        End Select

    End Sub
    Private Function GetHeader(ByVal iVal As Integer) As String
        Return Floor(iVal / (60 / iIncrement)) & ":" & IIf((((iVal Mod (60 / iIncrement)) * iIncrement) = 0), "00", (iVal Mod 60 / iIncrement) * iIncrement)
    End Function


    Private Sub dgvSchedule_CellValidated(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvSchedule.CellValidated

        If LockValidating Then
            Return
        End If
        If (From u In CType(sender, DataGridView).SelectedCells Select u).Any Then
            With CType(sender, DataGridView).SelectedCells(0)
                If .ColumnIndex = 1 And .ReadOnly = False Then
                    setDispo()

                End If
            End With
        End If
    End Sub


    Public Sub AddItem(ByVal strNom As String, ByVal oPlanEven As PlanificationEvenement, ByVal TypeEvenement As Outils.TypeEvenement, Optional ByVal Employe As Employe = Nothing)
        Dim bQuit As Boolean
        Dim oStartIndex As Integer = Outils.Horaire.GetBinFromHeure(CDate(oPlanEven.Heure_Debut))
        While Not bQuit
            Select Case TypeEvenement
                Case Outils.TypeEvenement.Activite
                    Dim oStrHoraire As String = CType(dgvSchedule.CurrentRow.Cells(0).Value, Horaire).strHoraire
                    Dim Chars() As Char
                    Chars = oStrHoraire
                    Chars(CInt(Outils.Horaire.GetBinFromHeure(GetHeader(oStartIndex)))) = "2"
                    oStrHoraire = Chars
                    dgvSchedule.CurrentRow.Cells(GetHeader(oStartIndex)).Style.BackColor = Outils.ParametreApplication.CouleurActivite
                    CType(dgvSchedule.CurrentRow.Cells(0).Value, Horaire).strHoraire = oStrHoraire
                    'MsgBox(oStrHoraire)
                Case Outils.TypeEvenement.Accompagnement
                    dgvSchedule.CurrentRow.Cells(GetHeader(oStartIndex)).Style.BackColor = Outils.ParametreApplication.CouleurAccompagnement
                Case Outils.TypeEvenement.Tache
                    dgvSchedule.CurrentRow.Cells(GetHeader(oStartIndex)).Style.BackColor = Outils.ParametreApplication.CouleurTache
            End Select
            dgvSchedule.Rows(0).Cells(GetHeader(oStartIndex)).ToolTipText = strNom

            If oStartIndex = Outils.Horaire.GetBinFromHeure(CDate(oPlanEven.Heure_Fin)) Then
                bQuit = True
            End If
            oStartIndex += 1
            If oStartIndex = 96 Then
                oStartIndex = 0
            End If


        End While

    End Sub



End Class


